package net.niufei.filter;

import net.niufei.Packet;
import net.niufei.SessionFilter;
import net.niufei.SessionFilterChain;
import net.niufei.dispatcher.Dispatcher;

public class DispatcherFilter implements SessionFilter {
	private final Dispatcher dispather;

	public DispatcherFilter(Dispatcher dispatcher) {
		this.dispather = dispatcher;
	}
	
	public Dispatcher getDispatcher() {
		return dispather;
	}
	
	
	@Override
	public void sessionStarted(final SessionFilterChain filterChain) throws Exception {
		dispather.dispatch(filterChain.getSession(), new Runnable() {
			@Override
			public void run() {
				filterChain.sessionStarted();
			}
		});
	}

	@Override
	public void sessionClosed(final SessionFilterChain filterChain) throws Exception {
		dispather.dispatch(filterChain.getSession(), new Runnable() {
			@Override
			public void run() {
				filterChain.sessionClosed();
			}
		});
	}

	@Override
	public void sessionTimeout(final SessionFilterChain filterChain) throws Exception {
		dispather.dispatch(filterChain.getSession(), new Runnable() {
			@Override
			public void run() {
				filterChain.sessionTimeout();
			}
		});
	}

	@Override
	public void packetReceived(final SessionFilterChain filterChain, final Packet packet)
			throws Exception {
		dispather.dispatch(filterChain.getSession(), new Runnable() {
			@Override
			public void run() {
				filterChain.packetReceived(packet);
			}
		});
	}

	@Override
	public void objectReceived(final SessionFilterChain filterChain, final Object obj)
			throws Exception {
		dispather.dispatch(filterChain.getSession(), new Runnable() {
			@Override
			public void run() {
				filterChain.objectReceived(obj);
			}
		});
	}

	@Override
	public void packetSend(final SessionFilterChain filterChain, final Packet packet)
			throws Exception {
		dispather.dispatch(filterChain.getSession(), new Runnable() {
			@Override
			public void run() {
				filterChain.packetSend(packet);
			}
		});
	}

	@Override
	public void packetSent(final SessionFilterChain filterChain, final Packet packet)
			throws Exception {
		dispather.dispatch(filterChain.getSession(), new Runnable() {
			@Override
			public void run() {
				filterChain.packetSent(packet);
			}
		});
	}

	@Override
	public void objectSent(final SessionFilterChain filterChain, final Object obj)
			throws Exception {
		dispather.dispatch(filterChain.getSession(), new Runnable() {
			@Override
			public void run() {
				filterChain.objectSent(obj);
			}
		});
	}

	@Override
	public void exceptionCaught(final SessionFilterChain filterChain, final Throwable cause) {
		dispather.dispatch(filterChain.getSession(), new Runnable() {
			@Override
			public void run() {
				filterChain.exceptionCaught(cause);
			}
		});
	}

}
